<?php
/**
 * View_Form
 *
 * класс Form_Form позволяет работать с формами
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 2.1
 */

class Form_ViewForm extends Form_Form {

  protected $_fullWidth = null;
  protected $_leftWidth = null;
  protected $_helpUnderField = false;

	public function __construct($arrSettings = array(), $objAR = null) {
  	parent::__construct($arrSettings, $objAR);
  	if ($this->_leftWidth) {
  		if ($this->_class)  $this->_class .= ' strictWidth';
  		else  $this->_class = 'strictWidth';
  	}
  }

    /**
	 * Метод устанавливает свойство
	 *
	 * @param string $settingName
	 * @param mixed $settingValue
	 */
	public function setSetting($settingName, $settingValue, $additionalAllowedSettings = null) {
    switch ($settingName) {
    	case 'fullWidth':
    		$this->setFullWidth($settingValue);
    		break;
    	case 'leftWidth':
    		$this->setLeftWidth($settingValue);
    		break;
    	case 'helpUnderField':
    		$this->_helpUnderField = $settingValue;
    		break;
    	default:
    	  parent::setSetting($settingName, $settingValue, '"action", "enctype", "successMessage"');
			  break;
    }
	}

	/**
	 * Устанавливает общую ширину формы
	 *
	 * @param string $fullWidth
	 */
	private function setFullWidth($fullWidth) {
		$this->_fullWidth = $fullWidth;
	}
	/**
	 * Устанавливает ширину левого столбца
	 *
	 * @param string $leftWidth
	 */
	private function setLeftWidth($leftWidth) {
		$this->_leftWidth = $leftWidth;
	}

	/**
	 * Выводит тег <form> и иже с ним
	 *
	 */
	public function show($justPlain = false) {
	  parent::show();
		$style = '';
		if ($this->_fullWidth) {
		  if (substr($this->_fullWidth, -1) == '%') $style = 'style="width:' . $this->_fullWidth  . ' !important;"';
		  else $style = 'style="width:' . $this->_fullWidth  . 'px !important;"';
		}
		if (!$justPlain) echo '<table cellspacing="0" ' . $style . '>';
	}
	/**
	 * Выводит тег </form> и иже с ним
	 *
	 */
	public function close($justPlain = false) {
		if (!$justPlain) echo '</table>';
		parent::close();
	}

	/**
	 * Выводит то, что должно быть написано перед полем
	 *
	 * @param array $arrSettings
	 * @param string $fieldName
	 */
	public function showBeforeField(array $arrSettings = array(), $fieldName = null) {
		$styleLeft = '';
		$styleRight = '';
		if ($this->_leftWidth) {
		  $styleLeft = 'style="width:' . $this->_leftWidth  . 'px !important;"';
		  $styleRight = 'style="width:auto !important;"';
		}
		if (!isset($arrSettings['rowspan1'])) echo '<tr ' . (isset($arrSettings['hiddenRow']) && $arrSettings['hiddenRow'] ? 'style="display:none;"' : null) . '><td class="left" ' . $styleLeft . '>';
		else echo '<tr><td class="left" rowspan="' . $arrSettings['rowspan1'] . '" ' . $styleLeft . '>';
	  if (isset($arrSettings['text'])) echo $arrSettings['text'];
	  if (isset($arrSettings['required']) && $arrSettings['required']) echo '<span class="required">*</span>';
	  if (isset($arrSettings['text'])) echo ':';
	  if (isset($arrSettings['help']) && !$this->_helpUnderField) echo '<p class="help">' . $arrSettings['help'] . '</p>';
		if (!isset($arrSettings['rowspan2'])) echo '</td><td class="right" '. $styleRight . '>';
		else echo '</td><td class="right" rowspan="' . $arrSettings['rowspan2'] . '" '. $styleRight . '>';

	}
	/**
	 * Выводит то, что должно быть написано после поля
	 *
	 * @param array $arrSettings
	 * @param string $fieldName
	 */
	public function showAfterField(array &$arrSettings = array(), $fieldName = null) {
		if (isset($arrSettings['help']) && $this->_helpUnderField)
		  echo '<p class="help">' . $arrSettings['help'] . '</p>';
		echo '</td></tr>';
	}
	/**
	 * Формирует и выводит текстовый элемент ввода
	 *
	 * @param array $arrSettings
	 */
	public function textField(array $arrSettings = array(), $fieldName = null, $onlyField = false) {
		if (!isset($arrSettings['class'])) $arrSettings['class'] = 'standardText';
		if (!$onlyField) $this->showBeforeField(&$arrSettings, &$Fieldname);
		parent::textField(&$arrSettings, &$fieldName);
		if (!$onlyField) $this->showAfterField(&$arrSettings, &$Fieldname);
	}
	/**
	 * Формирует и выводит элемент select
	 *
	 * @param array $arrSettings
	 */
	public function selectField(array $arrSettings = array(), $fieldName = null, $onlyField = false) {
		if (!isset($arrSettings['class'])) $arrSettings['class'] = 'standardSelect';
		if (!$onlyField) $this->showBeforeField(&$arrSettings, &$Fieldname);
		parent::selectField(&$arrSettings, &$fieldName);
		if (!$onlyField) $this->showAfterField(&$arrSettings, &$Fieldname);
	}

	/**
	 * Формирует и выводит элемент ввода файла
	 *
	 * @param array $arrSettings
	 */
	public function fileField(array $arrSettings = array(), $fieldName = null, $onlyField = false) {
		if (!$onlyField) $this->showBeforeField(&$arrSettings, &$Fieldname);
		if (isset($arrSettings['showImage'])) {
		  $folder = isset($arrSettings['showImage']['folder']) ? $arrSettings['showImage']['folder'] : null;
		  $file = isset($arrSettings['showImage']['file']) ? $arrSettings['showImage']['file'] : null;
		  $type = isset($arrSettings['showImage']['type']) ? $arrSettings['showImage']['type'] : '';
		  $style = isset($arrSettings['showImage']['style']) ? $arrSettings['showImage']['style'] : '';
		  if ($folder && $file) {
		    Image::showSavedImage($folder, $file, $type, $style);
		    echo '<br />';
		  }
		  unset($arrSettings['showImage']);
		}
		parent::fileField(&$arrSettings, &$fieldName);
		if (!$onlyField) $this->showAfterField(&$arrSettings, &$Fieldname);
	}
	/**
	 * Формирует и выводит элемент textarea
	 *
	 * @param array $arrSettings
	 */
	public function textareaField(array $arrSettings = array(), $fieldName = null, $onlyField = false) {
		if (!isset($arrSettings['class']))
		  $arrSettings['class'] = 'standardTextarea';
		if (!$onlyField) $this->showBeforeField(&$arrSettings, &$Fieldname);
		parent::textareaField(&$arrSettings, &$fieldName);
		if (!$onlyField) $this->showAfterField(&$arrSettings, &$Fieldname);
	}
	/**
	 * Формирует и выводит элемент wysiwyg
	 *
	 * @param array $arrSettings
	 */
	public function wysiwygField(array $arrSettings = array(), $fieldName = null, $onlyField = false) {
		if (!isset($arrSettings['class']))
		  $arrSettings['class'] = 'standardTextarea';
		if (!$onlyField) $this->showBeforeField(&$arrSettings, &$Fieldname);
		parent::wysiwygField(&$arrSettings, &$fieldName);
		if (!$onlyField) $this->showAfterField(&$arrSettings, &$Fieldname);
	}
  /**
	 * Формирует и выводит текстовый элемент ввода пароля
	 *
	 * @param array $arrSettings
	 */
	public function passwordField(array $arrSettings = array(), $fieldName = null, $onlyField = false) {
		if (!$onlyField) $this->showBeforeField(&$arrSettings, &$Fieldname);
		parent::passwordField(&$arrSettings, &$fieldName);
		if (!$onlyField) $this->showAfterField(&$arrSettings, &$Fieldname);
	}
  /**
	 * Формирует и выводит чекбокс
	 *
	 * @param array $arrSettings
	 */
	public function checkbox(array $arrSettings = array(), $fieldName = null, $onlyField = false) {
		if (!$onlyField) $this->showBeforeField(&$arrSettings, &$Fieldname);
		parent::checkbox(&$arrSettings, &$fieldName);
		if (!$onlyField) $this->showAfterField(&$arrSettings, &$Fieldname);
	}

  /**
	 * Формирует и выводит кнопку сабмит
	 *
	 * @param array $arrSettings
	 */
	public function submitButton(array $arrSettings = array(), $fieldName = null, $onlyField = false) {
		$styleLeft = '';
		$styleRight = '';
		if ($this->_leftWidth) {
		  $styleLeft = 'style="width:' . $this->_leftWidth  . 'px !important;"';
		  $styleRight = 'style="width:auto !important;"';
		}
		if (!isset($arrSettings['class'])) $arrSettings['class'] = 'standardSubmit';
		if (!$onlyField) {
			if (isset($arrSettings['show_on_left'])) {
			  echo '<tr><td colspan="2">';
			  unset($arrSettings['show_on_left']);
			}
			else {
  		  echo '<tr><td class="left" ' . $styleLeft . '>';
  			  if (isset($arrSettings['text'])) echo $arrSettings['text'];
  			echo '</td><td class="right" ' . $styleRight . '>';
			}
		}
		parent::submitButton(&$arrSettings, &$fieldName);
		if (!$onlyField) echo '</td></tr>';
	}

  /**
	 * Формирует и выводит Подзаголовок в форме
	 *
	 * @param array $arrSettings
	 */
	public function header3($headerText) {
		echo '<tr><td colspan="2" class="header3">';
			echo '<h3>' . $headerText . '</h3>';
		echo '</td></tr>';
	}


}